package com.bgs.controller;
import com.bgs.pojo.RegisterInfo;
import com.bgs.pojo.UserUpdatePassword;
import com.bgs.pojo.UserVo;
import com.bgs.service.LoginService;
import com.bgs.tool.*;
import com.bgs.utils.EmailUtil;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


/**
 * @program: xkgc
 * @description:
 * @author: Mr.Zhao
 * @create: 2022-05-09 15:19
 */
@RestController
@RequestMapping("/zsl")
public class LoginController {

    Logger log= LogManager.getLogger();

    @Autowired
    private LoginService loginService;

    @Autowired
    private EmailUtil emailUtil;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private AliOss aliOss;


    String file1=null;
    String file2=null;

    /**
     * @MethodName:   login
     * @Description: (登录方法)
     * @Params: [s]
     * @return: com.bgs.tool.R
     * @author: Mr.Zhao
     * @date:2022/5/13 9:32
     */
    @PostMapping("/login")
    public R login(@RequestBody UserVo s){

        UserVo user = loginService.login(s);
        System.out.println("-------------"+user);
        if (user != null){
            return R.ok().message("登陆成功").data("user",user);
        }else {
            return R.error().message("登录失败，用户名或密码错误");
        }
    }



    /**
     * @MethodName:   updatePassword
     * @Description: (忘记密码)
     * @Params: [upp]
     * @return: com.bgs.tool.R
     * @author: Mr.Zhao
     * @date:2022/5/13 9:33
     */
    @PostMapping("/updatePassword")
    public R updatePassword(@RequestBody UserUpdatePassword upp){


        String sss = redisUtil.get(upp.getEmails()).toString();
        System.out.println("-------------"+sss);
        if (sss==null || sss.equals("")){

            return R.ok().message("该验证码错误");
        } else if (redisUtil.get(upp.getEmails()).toString().equals(upp.getEmailCode())){

            loginService.uup(upp);
            return R.ok().message("修改成功");

        }else {

            return R.error().message("该验证码错误");
        }

    }

    /**
     * @MethodName:   registerInfo
     * @Description: (注册的信息进行添加,需要redis)
     * @Params: [registerInfo]
     * @return: com.bgs.tool.R
     * @author: Mr.Zhao
     * @date:2022/5/13 9:36
     */
    @PostMapping("/registerInfo")
    public R registerInfo(@RequestBody RegisterInfo registerInfo){

               registerInfo.setPicture1(file1);
               registerInfo.setPicture2(file2);

        String lnglat = registerInfo.getLnglat();
        String[] split = lnglat.split(",");
            registerInfo.setLongitude(split[0]);
           registerInfo.setDimensionality(split[1]);

        String sss = redisUtil.get(registerInfo.getEmail()).toString();
        System.out.println("-------------"+sss);

        if (2==2){
            loginService.registerInfo(registerInfo);
            return R.ok().message("注册成功");
        }else {
           return R.error().message("注册失败，信息有误或者验证码失效");
        }
    }



    /**
     * @MethodName:   zcGetCheckCode
     * @Description: (注册的邮箱验证码)
     * @Params: [email]
     * @return: com.bgs.tool.R
     * @author: Mr.Zhao
     * @date:2022/5/13 9:37
     */
    @PostMapping("/zcGetCheckCode")
    public R zcGetCheckCode(@RequestBody String email){

        System.out.println("发送的邮箱为==============="+email);
        String checkCode = String.valueOf((int)((Math.random()*9+1)*100000));
        log.info("验证码为==================="+checkCode);


            redisUtil.set(email, checkCode, 180);
            String message = "您的注册验证码为："+checkCode +"有效时长3分钟";
            emailUtil.sendSimpleMail(email, "注册验证码", message);

            return R.ok().message("邮件发送成功，请查收邮件");


    }


    /**
     * @MethodName:   getCheckCode
     * @Description: (忘记密码的邮箱验证,需要redis)
     * @Params: [emails]
     * @return: com.bgs.tool.R
     * @author: Mr.Zhao
     * @date:2022/5/13 9:34
     */
    @PostMapping("/getCheckCode")
    public R getCheckCode(@RequestBody String emails){


        System.out.println("发送的邮箱为==============="+emails);
        String checkCode = String.valueOf((int)((Math.random()*9+1)*100000));
        log.info("验证码为==================="+checkCode);
        Integer email = loginService.getEmails(emails);

        if (email==1){
            redisUtil.set(emails, checkCode, 180);
            String message = "您的注册验证码为："+checkCode +"有效时长3分钟";
            emailUtil.sendSimpleMail(emails, "注册验证码", message);
           return R.ok().message("邮件发送成功，请查收邮件");
        }
        else {
            return R.error().message("发送失败,无此邮箱");
        }
    }


    /**
     * @MethodName:   upload
     * @Description: (门店logo图片路径)
     * @Params: [file]
     * @return: java.lang.String
     * @author: Mr.Zhao
     * @date:2022/5/13 9:35
     */
    @RequestMapping("/upload")
    public String upload(MultipartFile file){

         file1=aliOss.upload(file);
        return file1;
    }


    /**
     * @MethodName:   uploadId
     * @Description: (身份证图片路径)
     * @Params: [file]
     * @return: java.lang.String
     * @author: Mr.Zhao
     * @date:2022/5/13 9:35
     */
    @RequestMapping("/uploadId")
    public String uploadId(MultipartFile file){

         file2=aliOss.upload(file);
        return file2;
    }

    /**
     * @MethodName:   card1
     * @Description: (身份证的信息识别)
     * @Params: [url1]
     * @return: java.lang.String
     * @author: Mr.Zhao
     * @date:2022/5/13 9:35
     */
    @RequestMapping("/card1")
    public static String card1(String url1){

//        url1="http://zgd-upload.oss-cn-beijing.aliyuncs.com";
        System.out.println("qqqqqq"+url1);
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
        try {

            String param = "id_card_side=" + "front" + "&url=" + url1;
            // 注意这里仅为了简化编码每一次请求都去获取access_token，线上环境access_token有过期时间， 客户端可自行缓存，过期后重新获取。
            String accessToken = "24.960b8853925f180fd5ff9b0a90b730a7.2592000.1654342784.282335-26161801";
            //调用工具类发请求
            String result = HttpUtil.post(url, accessToken, param);
            System.out.println("--------"+result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }






}
